package com.hs.controller;


import com.hs.entity.TbUser;
import com.hs.service.UserService;

import com.hs.utils.MD5Utils;
import com.hs.utils.MailUtils;
import com.hs.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/studentList")
    public String studentList(){
        return "manager/student-list";
    }

    @RequestMapping("/teacherList")
    public String teacherList(){
        return "manager/teacher-list";
    }

    @RequestMapping("/managerList")
    public String managerList(){
        return "manager/manager-list";
    }

    @RequestMapping("studentList/data")
    @ResponseBody
    public Map studentListJson(){
        List<TbUser> list=userService.findUserListByRoleId(3);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    @RequestMapping("teacherList/data")
    @ResponseBody
    public Map teacherListJson(){
        List<TbUser> list=userService.findUserListByRoleId(2);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    @RequestMapping("managerList/data")
    @ResponseBody
    public Map managerListJson(){
        List<TbUser> list=userService.findUserListByRoleId(1);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    @RequestMapping("/accountList")
    public String userList(){
        return "manager/account-list";
    }

    @RequestMapping("/forgetme")
    public void forget(){
        System.out.println(12);
    }

    /*跳转到添加页面*/
    @RequestMapping("/accountAdd")
    private String accountAdd(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleId=request.getParameter("roleId");
        model.addAttribute("roleId",roleId);
        return "manager/account-add";
    }

    /*添加用户*/
    @RequestMapping("/createAccount")
    private void createAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        String realName=request.getParameter("realName");
        String password=request.getParameter("password");
        String roleId= request.getParameter("roleId");
        Integer row=userService.createAccount(realName,password,Integer.parseInt(roleId));
        if (row==1){
            ResultUtils.success(response,"创建成功");
        }else {
            ResultUtils.error(response,321,"创建失败");
        }
    }

    @RequestMapping("/accountEdit")
    private String accountEdit(Model model,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String id=request.getParameter("id");
        TbUser user=userService.queryById(Integer.valueOf(id));
        model.addAttribute("userInfo",user);
        return "manager/account-edit";
    }

    @RequestMapping("/updateUserForStudent")
    private void updateUserForStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,String[]> params=request.getParameterMap();
        response.setCharacterEncoding("UTF-8");
        try {
            Integer rows=userService.updateUserForStudent(params);
            if (rows==1){
                ResultUtils.success(response,"修改成功");
            }else {
                ResultUtils.error(response,2,"修改失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            ResultUtils.error(response,1,"系统异常，请联系管理员");
        }
    }

    @RequestMapping("resetPassword")
    private void resetPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email=request.getParameter("email");
        String id = request.getParameter("id");
        response.setCharacterEncoding("UTF-8");
        try {
            Integer password= MailUtils.sendPassword(email);
            Integer rows=userService.updatePasswordById(MD5Utils.getMD5(String.valueOf(password)), Integer.valueOf(id));
            if(rows==1){
                ResultUtils.success(response,"密码重置成功，请查看邮件");
            }else {
                ResultUtils.error(response,43,"密码重置失败");
            }
        }catch (SQLException e){
            e.printStackTrace();
            ResultUtils.error(response,41,"数据库异常，请联系管理员");
        }catch (NumberFormatException e){
            e.printStackTrace();
            ResultUtils.error(response,41,"参数异常，请联系管理员");
        }
        catch (Exception e) {
            e.printStackTrace();
            ResultUtils.error(response,40,"邮件发送失败,请检查邮箱格式");
        }
    }

}
